#!/bin/bash

listArr=(/root/share /root/test_share2 /root/test_share3)
currTime=$(date "+%Y-%m-%d %H:%M:%S")

check_mount(){
  ls $1 > /dev/null 2>&1
  if [ $? != 0 ]
  then
    return 1
  else
    mount | grep $1 > /dev/null
    if [ $? != 0 ]
    then
      return 1
    fi
  fi
  return 0
}

run_mount(){
  mount | grep $1 > /dev/null
  if [ $? == 0 ]
  then
    umount $1
  fi
  case $1 in
  /root/share)
    mount -t cifs -o username="",password= //192.168.56.104/share /root/share > /tmp/check_mount_error 2>&1
    ;;
  /root/test_share2)
    mount -t cifs -o username="",password= //192.168.56.104/test_share /root/test_share2 > /tmp/check_mount_error 2>&1
    ;;
  /root/test_share3)
    mount -t cifs -o username="",password= //192.168.56.104/Users /root/test_share3 > /tmp/check_mount_error 2>&1
  esac
  #mount -t cifs -o username=$3,password=$4 $2 $1 > /tmp/check_mount_error 2>&1
  return $?
}

case $1 in
stop)
  for((i = 0;i<${#listArr[@]};i++)) do
      umount ${listArr[i]}
  done
  exit 0
  ;;
start)
  for((i = 0;i<${#listArr[@]};i++)) do
    check_mount ${listArr[i]}
    if [ $? != 0 ]
    then
        run_mount ${listArr[i]} ${netpathArr[i]} ${userArr[i]} ${passwdArr[i]}
        if [ $? == 0 ]
        then
          echo ${currTime}"|本地目录："${listArr[i]}" 挂载成功"
        else
          echo ${currTime}"|本地目录："${listArr[i]}" 挂载失败("`cat /tmp/check_mount_error`")"
        fi
    else
      continue
    fi
  done
  ;;
list)
  mount
  ;;
*)
  echo $0 "(start|list|stop)"
  ;;
esac
